package com.zjmzxfzhl.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.zjmzxfzhl.modules.app.form.AppProjUserVo;
import com.zjmzxfzhl.modules.app.form.AppTermUserVo;
import com.zjmzxfzhl.modules.app.form.AppTermVo;
import com.zjmzxfzhl.modules.sys.entity.SysUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.entity.AppTermUser;
import com.zjmzxfzhl.modules.app.mapper.AppTermUserMapper;
import com.zjmzxfzhl.modules.app.service.AppTermUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppTermUserServiceImpl extends BaseServiceImpl<AppTermUserMapper, AppTermUser>
        implements AppTermUserService {
    @Override
    public IPage<AppTermUserVo> list(IPage<AppTermUserVo> page, AppTermUserVo appTermUser) {
        return page.setRecords(baseMapper.list(page, appTermUser));
    }
    @Override
    public List<AppTermUserVo> getList(IPage<AppTermUserVo> page, AppTermUserVo appTermUser) {
        return baseMapper.list(page, appTermUser);
    }
    public  List<SysUser> getTermUserList(AppTermUserVo appTermUserVo) {
        return baseMapper.getTermUserList(appTermUserVo);
    }
    public  List<SysUser> getOtherUserList(AppTermUserVo appTermUserVo) {
        return baseMapper.getOtherUserList(appTermUserVo);
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appTermUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppProj(AppTermUser appTermUser) {
        Map<SFunction<AppTermUser,?>,Object> params=new HashMap<>();
        if(appTermUser.getTermId()!=null&&!"".equals(appTermUser.getTermId())){
            params.put(AppTermUser::getTermId,appTermUser.getTermId());
        }
        if(appTermUser.getUserId()!=null&&!"".equals(appTermUser.getUserId())){
            params.put(AppTermUser::getUserId,appTermUser.getUserId());
        }
        Integer total=this.count(new LambdaQueryWrapper<AppTermUser>().allEq(params));

        if(total>0){
            return false;
        }
        Boolean result=this.save(appTermUser);
        return result;
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appTermUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean relationDel(AppTermUser appTermUser) {
        Map<String, Object> columnMap=new HashMap<>();
        columnMap.put("TERM_ID",appTermUser.getTermId());
        columnMap.put("USER_ID",appTermUser.getUserId());
        Boolean result=this.removeByMap(columnMap);
        return result;
    }

    /**
     * 批量添加成员
     * @param termId
     * @param userIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAdd(String termId,String userIds) {
        String[] idsArr = userIds.split(",");
        for (String id:idsArr) {
            AppTermUser appTermUser=new AppTermUser();
            appTermUser.setTermId(termId);
            appTermUser.setUserId(id);
            boolean bool=saveAppProj(appTermUser);
            if(bool==false){
                return false;
            }
        }
        return true;
    }
}
